<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\lib\exception\BaseException;
use app\middleware\admin\Auth;
use app\model\Admins;
use app\model\ClassTime;
use app\model\Role;
use app\model\Rule;
use app\model\School;

class Admin extends BaseController
{
    protected $middleware = [Auth::class];

    /**
     * 菜单
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getinfo()
    {
        $role = Admins::with('role')->find(request()->userid);
        if ($role->role_id == 1) {
            $menu = Rule::where('is_menu', 1)->where('status', 1)->order('sort', 'desc')->select();
            $authority = Rule::where('status', 1)->where('is_menu', 0)->column('path');
        } else {
            $rule = Role::find($role->role_id);
            $menu = Rule::where('is_menu', 1)->where('status', 1)->order('sort', 'desc')->whereIn('id', $rule->rule_id)->select();
            $authority = Rule::where('is_menu', 0)->where('status', 1)->order('sort', 'desc')->whereIn('id', $rule->rule_id)->column('path');
        }
        $role['menu'] = getTree($menu);

        $role['school'] = School::where('id', 'in', $role->school_id)->select();
        $role['defaultSchool'] = School::where('id', $role->belong_school)->find();
        $role['rule'] = $authority;
        return $this->success($role);
    }

    /**获取学校
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSchool()
    {
        $school_id = request()->param('school_id');
        $admin = Admins::where('id', request()->userid)->find();
        if (empty($school_id)) {
            $data['defaultSchool'] = School::where('id', $admin->belong_school)->find();
        } else {
            $data['defaultSchool'] = School::where('id', $school_id)->find();
        }
        $data['school'] = School::where('id', 'in', $admin->school_id)->select();
        return $this->success($data);
    }

    /**
     * 教师职员资料
     * @param int $limit
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index($limit = 10)
    {
        $name = request()->param('name');
        $status = request()->param('status');
        $time = request()->param('time');
        $school_id = request()->param('school_id');
        $data = Admins::where('belong_school',$school_id)
            ->with('role')->when($name, function ($query) use ($name) {
                $query->where('username', 'like', "%$name%");
            })->when($status, function ($query) use ($status) {
                $query->where('status', $status);
            })->when($time, function ($query) use ($time) {
                $query->whereBetween('create_time', $time);
            })->order('id', 'asc')->paginate($limit);
        return $this->success($data);

    }

    public function create()
    {
        $params = request()->param();
        if (!empty($params['password'])) {
            $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
        }
       $username= Admins::where('username',$params['username'])->find();
       if($username){
           throw new BaseException(['msg'=>'該賬號已存在！']);
       }
        Admins::create($params);
        return $this->success();
    }

    public function update()
    {
        $params = request()->param();
        if (!empty($params['password'])) {
            $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
        }else{
            unset($params['password']);
        }
        $model = Admins::where('id', $params['id']);
        unset($params['id']);
        return $this->success($model->update($params));
    }

    public function delete($id)
    {
        Admins::where('id', 'in', $id)->delete();
        return $this->success();
    }

    /**
     * 老師報表
     * @return void
     */
    public function teacher($limit=15)
    {
        $time = request()->param('time');
        $admin_id=request()->param('admin_id');
        $teacher= Admins::where('role_id',5)->field('id')->select()->toArray();
        $tids=array_column($teacher,'id');

        $data= ClassTime::with(['course','teacher'])
           ->when($time, function ($query) use ($time) {
               $query->whereBetween('start_time', $time);
           })
           ->when($admin_id, function ($query) use ($admin_id) {
               $query->whereBetween('admin_id', $admin_id);
           })
           ->where('admin_id','in',$tids)

           ->paginate($limit)->toArray();

       $item=array_column($data['data'],'admin_id');
       $count=array_count_values($item);
        foreach ($data['data'] as &$v){
            $v['class_count']=$count[$v['admin_id']];
        }

        return $this->success($data);
    }
}
